
package geneontofx.association;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.concurrent.ConcurrentHashMap;


public class AssociationsModel 
{
    private final ConcurrentHashMap<String, ArrayList<Association>> associationsMap;
    
    private static final String DEF_PATH = "src\\_data\\gene_association.goa_human";
    private String filePath;
    
    private boolean parsed;
    
    private static AssociationsModel associationsInstance = null;

    private AssociationsModel() {
        associationsMap = new ConcurrentHashMap<>();
    }
    
    public static AssociationsModel getAssociationsModelInstance() {
        if(associationsInstance == null) {
            associationsInstance = new AssociationsModel();
        }
        return associationsInstance;
    }
    
    public void parseAssociations(String path) throws IOException {
        if(path == null) path = DEF_PATH;
        
        BufferedReader input;
        try {
            input = new BufferedReader(new FileReader(path));
            
            String line;
            String[] splited;
            Association currAssociation;
            ArrayList<Association> currAssociationList;
            while ((line = input.readLine()) != null) {
                if(line.startsWith("!")) {
                } else {
                    splited = line.split("\t");
                    currAssociation = new Association(splited[0], splited[1], splited[3], splited[4], splited[9]);
                    if(associationsMap.containsKey(currAssociation.getDbId())) {
                        currAssociationList = associationsMap.get(currAssociation.getDbId());
                        currAssociationList.add(currAssociation);
                        associationsMap.put(currAssociation.getDbId(), currAssociationList);
                    }
                    else {
                        currAssociationList = new ArrayList<>();
                        currAssociationList.add(currAssociation);
                        associationsMap.put(currAssociation.getDbId(), currAssociationList);
                    }
                }
            }
            input.close();
        } catch (FileNotFoundException fileNotFoundEx){
            System.err.format("File Not Found: %s%n", fileNotFoundEx);
        }
        setParsed(true);
    }

    public ArrayList<Association> getSingleAssociationList(String dbId) {
        return associationsMap.get(dbId);
    }

    public boolean isParsed() {
        return parsed;
    }

    public void setParsed(boolean parsed) {
        this.parsed = parsed;
    }

    public String getFilePath() {
        return filePath;
    }

    public void setFilePath(String filePath) {
        this.filePath = filePath;
    }
}
